package com.easyse.serviceuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyse.model.DO.User;
import com.easyse.model.vo.ResultVO;
import com.easyse.serviceclient.clients.SystemClient;
import com.easyse.serviceuser.service.UserService;
import com.easyse.serviceuser.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.sql.Wrapper;
import java.util.List;

/**
* @author 25405
* @description 针对表【common_user】的数据库操作Service实现
* @createDate 2022-10-05 17:44:32
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{


    @Autowired
    SystemClient systemClient;

    @Autowired
    UserMapper userMapper;

    @Override
    public ResultVO addUser(User user) {
        int insert = userMapper.insert(user);
        if(insert ==1 ){
            return  ResultVO.success("增加用户成功！");
        }else{
            return ResultVO.failure("添加用户失败！");
        }
    }

    @Override
    public ResultVO searchUserlikeName(String string) {
        ResultVO resultVO ;
        //queryWrapper组装查询where条件
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        string = string + "%";
        userWrapper.select("id", "username", "phonenumber", "email").like("username",string);
        List<User> users = userMapper.selectList(userWrapper);
        if(ObjectUtils.isEmpty(users)){
            resultVO = ResultVO.failure("没有找到用户！");
        }else{
            resultVO = ResultVO.success("查找成功!");
            resultVO.data("users",users);
        }
        return resultVO;
    }

    @Override
    public List<String> selectPermsByUserId (Long id){
        return systemClient.selectPermsByUserId(id);

    }

    @Override
    public ResultVO deleteUser(Integer id) {
        User user = userMapper.selectById(id);
        //删除用户 设置标志位1
        user.setIsDeleted(1);
        int i = userMapper.updateById(user);
        if( i == 1){
        return ResultVO.success("删除用户成功！");
        }
        else{
            return ResultVO.failure("发生错误！用户删除失败！");
        }
        }

    @Override
    public ResultVO getUserList(Integer pageNum) {
        //分页

        //分页参数
        Page<User> rowPage = new Page(pageNum, 10);

        //queryWrapper组装查询where条件
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.select("id", "username", "phonenumber", "email");
        rowPage = userMapper.selectPage(rowPage, userWrapper);
        if (ObjectUtils.isEmpty(rowPage)) {
            return ResultVO.failure("获取用户列表失败!");
        } else {
            ResultVO resultVO;
            resultVO = ResultVO.success("获取用户列表成功");
            resultVO.data("users",rowPage);
            return  resultVO;
        }
    }

    @Override
    public ResultVO updateUser(User user) {
        User u = userMapper.selectById(user.getId());
        if(ObjectUtils.isEmpty(u)){
            //查询用户id为空
            return ResultVO.failure("更新失败，请检查用户ID！");
        }else{
            //找到用户了 更新他的信息
            int i = userMapper.updateById(user);
            if(i == 1){
                //更新成功
                return  ResultVO.success("更新用户信息成功！");
            }
        }
//        return null;
        return ResultVO.failure("发生未知错误!用户信息更新失败！");

    }
}




